import sys,os
import cv2
from math import ceil,floor

try:
    input_file = sys.argv[1]
except:
    input_file = 'face.jpg'
try:
    output_file = sys.argv[2]
except:
    output_file = 'encoded_face.jpg'



def encode_mac(mac):
    encoded_mac = mac.split(':')
    return encoded_mac

def encode_face(media,encoded_mac):
    def encode_single(info):
        g = floor(hex2dec(info)/15)
        b = hex2dec(info)%15
        r = int((hex2dec(info)+mean)/2)
        return [g,b,r]

    height,width,_ = media.shape
    center = get_center(media)
    xp,yp = center
    TL = [0,0]
    TC = [0,xp]
    TR = [0,width-1]
    BL = [height-1,0]
    BC = [height-1,xp]
    BR = [height-1,width-1]
    encode_points = [TL,TC,TR,BL,BC,BR]

    encoded_mac = [encode_single(info) for info in encoded_mac]
    tl,tc,tr,bl,bc,br = encoded_mac
    for index_point in range(len(encode_points)):
        for channel in range(0,3):
            media.itemset((encode_points[index_point][0],encode_points[index_point][1],channel),encoded_mac[index_point][channel])
    encoded_face = media

    return encoded_face

def decode_face(media):
    def decode_single(info):
        g,b,r = info
        mac_info_single = g*15+b
        return mac_info_single
    height,width,_ = media.shape
    center = get_center(media)
    xp,yp = center
    TL = [0,0]
    TC = [0,xp]
    TR = [0,width-1]
    BL = [height-1,0]
    BC = [height-1,xp]
    BR = [height-1,width-1]
    encode_points = [TL,TC,TR,BL,BC,BR]
    decoded_info = []
    for index_point in range(len(encode_points)):
        point_info = []
        for channel in range(0,3):
            point_info.append(media.item(encode_points[index_point][0],encode_points[index_point][1],channel))
        point_info = decode_single(point_info)
        decoded_info.append(dec2hex(point_info))
    decoded_info = ':'.join(decoded_info)
    
    return decoded_info
    
        

def get_center(img):
    height,width,_ = img.shape
    center = [ceil(height/2),ceil(width/2)]
    return center

def hex2dec(string_num):
    return int(string_num.upper(),16)

def dec2hex(string_num):
    base = [str(x) for x in range(10)] + [chr(x) for x in range(ord('A'),ord('A')+6)]
    num = int(string_num)
    mid = []
    while True:
        if num == 0: break
        num,rem = divmod(num,16)
        mid.append(base[rem])
    return ''.join([str(x) for x in mid[::-1]])

def calculate_mean(img):
    height,width,channels = img.shape
    pixels = height*width*channels
    whole = 0
    for index_h in range(height):
        for index_w in range(width):
            for index_c in range(channels):
                whole += img[index_h][index_w][index_c]
    mean = whole/pixels

    return mean

if __name__ == '__main__':
    media = cv2.imread(input_file)
    center = get_center(media)
    with open('mac.txt','r') as f:
        mac = f.readlines()[0].split('\n')[0]

    encoded_mac = encode_mac(mac)
    tl,bl,tr,br,le,re = encoded_mac
    mean = calculate_mean(media)
    encoded_face = encode_face(media,encoded_mac)
    decoded_mac = decode_face(encoded_face)
    print(decoded_mac)

    cv2.imwrite(output_file,encoded_face)
